<?php

  function getTitle($url, $url_param) {
    $return_string = "";

    $query = "SELECT id FROM WebPage WHERE url = '" . $url . "'";
    $result = mysql_query($query);
    $webpage_id = -1;
    if (mysql_num_rows($result) == 0) {
      $webpage_id = registerPage($url);
    } else {
      $result_row = mysql_fetch_assoc($result);
      $webpage_id = $result_row['id'];
    }

    $webpagequery_id = getPageParameterId($webpage_id, $url_param);
    $query = "SELECT id, title FROM WebPageQuery  WHERE id='$webpagequery_id'";
    $result = mysql_query($query);
    $result_row = mysql_fetch_assoc($result);

    $return_string .= "<title>" . $result_row['title'] . "</title>"
        . "<returncode>1</returncode>";

    return $return_string;
  }

  function updateTitle($url, $url_param, $title) {
    $return_string = "";

    if (!isset($_SESSION['user_name'])) {
      $return_string .= "<returncode>0</returncode>\n";
      $return_string .= "<errormessage>You're not logged in!</errormessage>\n";
      return $return_string;
    }

    $query = "SELECT id FROM WebPage WHERE url = '$url'";
    $result = mysql_query($query);
    $webpage_id = -1;
    if (mysql_num_rows($result) == 0) {
      $webpage_id = registerPage($url);
    } else {
      $result_row = mysql_fetch_assoc($result);
      $webpage_id = $result_row['id'];
    }

    $webpagequery_id = getPageParameterId($webpage_id, $url_param);
    $user_id = $_SESSION['user_id'];
    $query = "UPDATE WebPageQuery SET title='" 
        . mysql_real_escape_string($title) 
        . "' WHERE id=$webpagequery_id";
    $result = mysql_query($query);
    
    $return_string .= "<returncode>1</returncode>";

    return $return_string;
  }
?>
